﻿using System;
using GeoAPI.Geometries;

namespace NetTopologySuite.Algorithm
{
	public class HCoordinate
	{
		public static Coordinate Intersection(Coordinate p1, Coordinate p2, Coordinate q1, Coordinate q2)
		{
			double num = p1.Y - p2.Y;
			double num2 = p2.X - p1.X;
			double num3 = p1.X * p2.Y - p2.X * p1.Y;
			double num4 = q1.Y - q2.Y;
			double num5 = q2.X - q1.X;
			double num6 = q1.X * q2.Y - q2.X * q1.Y;
			double num7 = num2 * num6 - num5 * num3;
			double num8 = num4 * num3 - num * num6;
			double num9 = num * num5 - num4 * num2;
			double num10 = num7 / num9;
			double num11 = num8 / num9;
			if (double.IsNaN(num10) || double.IsInfinity(num10) || double.IsNaN(num11) || double.IsInfinity(num11))
			{
				throw new NotRepresentableException();
			}
			return new Coordinate(num10, num11);
		}

		public static Coordinate OldIntersection(Coordinate p1, Coordinate p2, Coordinate q1, Coordinate q2)
		{
			HCoordinate p3 = new HCoordinate(new HCoordinate(p1), new HCoordinate(p2));
			HCoordinate p4 = new HCoordinate(new HCoordinate(q1), new HCoordinate(q2));
			return new HCoordinate(p3, p4).Coordinate;
		}

		[Obsolete("This is a simple access to x private field: use GetX() instead.")]
		protected double X
		{
			get
			{
				return this._x;
			}
			set
			{
				this._x = value;
			}
		}

		[Obsolete("This is a simple access to y private field: use GetY() instead.")]
		protected double Y
		{
			get
			{
				return this._y;
			}
			set
			{
				this._y = value;
			}
		}

		[Obsolete("This is a simple access to w private field: how do you use this field for?...")]
		protected double W
		{
			get
			{
				return this._w;
			}
			set
			{
				this._w = value;
			}
		}

		public HCoordinate()
		{
			this._x = 0.0;
			this._y = 0.0;
			this._w = 1.0;
		}

		public HCoordinate(double x, double y, double w)
		{
			this._x = x;
			this._y = y;
			this._w = w;
		}

		public HCoordinate(Coordinate p)
		{
			this._x = p.X;
			this._y = p.Y;
			this._w = 1.0;
		}

		public HCoordinate(HCoordinate p1, HCoordinate p2)
		{
			this._x = p1._y * p2._w - p2._y * p1._w;
			this._y = p2._x * p1._w - p1._x * p2._w;
			this._w = p1._x * p2._y - p2._x * p1._y;
		}

		public double GetX()
		{
			double num = this._x / this._w;
			if (double.IsNaN(num) || double.IsInfinity(num))
			{
				throw new NotRepresentableException();
			}
			return num;
		}

		public double GetY()
		{
			double num = this._y / this._w;
			if (double.IsNaN(num) || double.IsInfinity(num))
			{
				throw new NotRepresentableException();
			}
			return num;
		}

		public Coordinate Coordinate
		{
			get
			{
				return new Coordinate(this.GetX(), this.GetY());
			}
		}

		public HCoordinate(Coordinate p1, Coordinate p2)
		{
			this._x = p1.Y - p2.Y;
			this._y = p2.X - p1.X;
			this._w = p1.X * p2.Y - p2.X * p1.Y;
		}

		public HCoordinate(Coordinate p1, Coordinate p2, Coordinate q1, Coordinate q2)
		{
			double num = p1.Y - p2.Y;
			double num2 = p2.X - p1.X;
			double num3 = p1.X * p2.Y - p2.X * p1.Y;
			double num4 = q1.Y - q2.Y;
			double num5 = q2.X - q1.X;
			double num6 = q1.X * q2.Y - q2.X * q1.Y;
			this._x = num2 * num6 - num5 * num3;
			this._y = num4 * num3 - num * num6;
			this._w = num * num5 - num4 * num2;
		}

		private double _x;

		private double _y;

		private double _w;
	}
}
